home *** CD-ROM | disk | FTP | other *** search
/ MacGames Sampler / PHT MacGames Bundle.iso / MacSource Folder / Samples from the CD / Editors / emacs / Emacs-1.14b1-sources / sources / utility-src / ispell / lexfsm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-04  |  4.1 KB  |  215 lines  |  [TEXT/EMAC]

  1. /* Copyright (C) 1990, 1993 Free Software Foundation, Inc.
  2.  
  3.    This file is part of GNU ISPELL.
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 2, or (at your option)
  8.    any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "stdio.h"
  20.  
  21. #ifdef HAVE_MALLOC_H
  22. #include <malloc.h>
  23. #endif
  24.  
  25. #include "ispell.h"
  26.  
  27. // extern added MP
  28. #define EXTERN1
  29. #define EXTERN2 extern
  30. EXTERN1 unsigned char **lextable;
  31. EXTERN1 unsigned char *lexchar;
  32. EXTERN2 char **lexdecode;
  33. EXTERN1 int lexletters;
  34.  
  35. /* if out is an odd number of bytes long, must zero the
  36.  * remaining byte because hash() works 16 bits at a time.
  37.  * therefore, always write 2 zeros
  38.  *
  39.  * return the number of bytes in the compessed output.
  40.  *
  41.  * If the string contains a character that is not in the lexchar[]
  42.  * array, then the word cannot possiblally be in the dictionary.
  43.  * Therefore return 0.
  44.  */
  45. int
  46. lexword (in, len, out)
  47.   char *in;
  48.   int len;
  49.   unsigned char *out;
  50. {
  51.   unsigned char c1, c2, code;
  52.   unsigned char *startout;
  53.   startout = out;
  54. newc1:
  55.   if (len-- == 0)
  56.     {
  57.       out[0] = 0;
  58.       out[1] = 0;
  59.       return (out - startout);
  60.     }
  61.  
  62.   c1 = lexchar[*(unsigned char *) in++];
  63.   if (c1 == 0)
  64.     {
  65.       /* this character was not used anywhere in the dictionary
  66.          * return 0 to tell lookup() not to bother looking in
  67.          * the hash table
  68.          */
  69.       return (0);
  70.     }
  71.  
  72.   if (len == 0)
  73.     {
  74.       *out++ = c1;
  75.       out[0] = 0;
  76.       out[1] = 0;
  77.       return (out - startout);
  78.     }
  79.  
  80.   while (len--)
  81.     {
  82.       /* c1 is a good letter */
  83.       c2 = lexchar[*(unsigned char *) in++];
  84.       code = lextable[c1][c2];
  85.       switch (code)
  86.     {
  87.     case 255:        /* c2 is not a letter */
  88.       return (0);
  89.     case 0:        /* both letters, but no code for this pair */
  90.       *out++ = c1;
  91.       c1 = c2;
  92.       break;
  93.     default:
  94.       *out++ = code;
  95.       goto newc1;
  96.     }
  97.     }
  98.   *out++ = c1;
  99.   out[0] = 0;
  100.   out[1] = 0;
  101.   return (out - startout);
  102. }
  103.  
  104.  
  105. void
  106. lexprint (out, verbose)
  107.   unsigned char *out;
  108.   int verbose;
  109. {
  110.   char *p;
  111.   char *left, *right;
  112.  
  113.   if (verbose)
  114.     {
  115.       left = "<";
  116.       right = ">";
  117.     }
  118.   else
  119.     {
  120.       left = "";
  121.       right = "";
  122.     }
  123.   while (*out)
  124.     {
  125.       p = lexdecode[*out];
  126.       if (*p == 0)
  127.     (void) printf ("{illegal code %d}", *out);
  128.       else if (p[1] == 0)
  129.     putchar (p[0]);
  130.       else
  131.     printf ("%s%s%s", left, p, right);
  132.       out++;
  133.     }
  134. }
  135.  
  136. int
  137. lexsize (NOARGS)
  138. {
  139.   return (256 * 2 + 256 + lexletters * lexletters);
  140. }
  141.  
  142.  
  143. void
  144. lexdump (f)
  145.   FILE *f;
  146. {
  147.   int i, j;
  148.  
  149.   for (i = 0; i < 256; i++)
  150.     {
  151.       (void) putc (lexdecode[i][0], f);
  152.       (void) putc (lexdecode[i][1], f);
  153.     }
  154.   for (i = 0; i < 256; i++)
  155.     (void) putc ((int) lexchar[i], f);
  156.  
  157.   for (i = 0; i < lexletters; i++)
  158.     for (j = 0; j < lexletters; j++)
  159.       (void) putc ((int) lextable[i][j], f);
  160. }
  161.  
  162.  
  163. void
  164. lexalloc (NOARGS)
  165. {
  166.   int i;
  167.   char *p;
  168.  
  169.  
  170.   lexdecode = (char **) xmalloc (256 * sizeof (char *));
  171.   p = (char *) xcalloc (256, 3);
  172.  
  173.   for (i = 0; i < 256; i++)
  174.     {
  175.       lexdecode[i] = p;
  176.       p += 3;
  177.     }
  178.  
  179.   lexchar = (unsigned char *) xcalloc (256, 1);
  180.   lextable = (unsigned char **) xmalloc (lexletters * sizeof (char *));
  181.  
  182.   for (i = 0; i < lexletters; i++)
  183.     lextable[i] = (unsigned char *) xcalloc ((unsigned) lexletters, 1);
  184. }
  185.  
  186.  
  187. void
  188. lexload (f, nlet)
  189.   FILE *f;
  190.   unsigned long nlet;
  191. {
  192.   int i, j;
  193.   unsigned char *up;
  194.   char *p;
  195.  
  196.   lexletters = (int) nlet;
  197.   lexalloc ();
  198.   for (i = 0; i < 256; i++)
  199.     {
  200.       p = lexdecode[i];
  201.       *p++ = getc (f);
  202.       *p++ = getc (f);
  203.       *p = 0;
  204.     }
  205.  
  206.   for (i = 0, up = lexchar; i < 256; i++, up++)
  207.     *up = getc (f);
  208.  
  209.   for (i = 0; i < lexletters; i++)
  210.     {
  211.       for (j = 0; j < lexletters; j++)
  212.     lextable[i][j] = getc (f);
  213.     }
  214. }
  215.